#!/usr/bin/python3
#
# Copyright (C) 2021 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.

import os
import sys

# import Cockpit's machinery for test VMs and its browser test API
TEST_DIR = os.path.dirname(__file__)
sys.path.append(os.path.join(TEST_DIR, "common"))
sys.path.append(os.path.join(os.path.dirname(TEST_DIR), "bots/machine"))

from testlib import MachineCase, nondestructive, test_main  # pylint: disable=import-error

USERS_INTERFACE = "org.fedoraproject.Anaconda.Modules.Users"
USERS_OBJECT_PATH = "/org/fedoraproject/Anaconda/Modules/Users"


@nondestructive
class TestRootAccount(MachineCase):

    def testBasic(self):
        b = self.browser
        m = self.machine

        bus_address = m.execute("cat /run/anaconda/bus.address")
        get_root_account_locked_cmd = (
            f'dbus-send --print-reply --bus="{bus_address}" \
              --dest={USERS_INTERFACE} \
              {USERS_OBJECT_PATH} \
              org.freedesktop.DBus.Properties.Get \
              string:"{USERS_INTERFACE}" string:"IsRootAccountLocked"'
        )
        set_root_account_locked_cmd = (
            lambda value: f'dbus-send --print-reply --bus="{bus_address}" \
              --dest={USERS_INTERFACE} \
              {USERS_OBJECT_PATH} \
              {USERS_INTERFACE}.SetRootAccountLocked \
              boolean:"{value}"'
        )

        b.open("/cockpit/@localhost/anaconda-webui/index.html")

        b.click('#continue-btn')
        b.click('#root-account')

        # Read IsRootAccountLocked property from the DBUS API
        # and make sure it matches the value displayed in the WebUI
        root_account_locked = m.execute(get_root_account_locked_cmd)
        self.assertIn("boolean true", root_account_locked)
        b.wait_visible("#root-account-lock:checked")

        # Change IsRootAccountLocked property from the WebUI
        # and make sure the change it reflected in the CLI
        b.set_checked("#root-account-lock", False)
        b.click('#header-done-btn')
        root_account_locked_new = m.execute(get_root_account_locked_cmd)
        self.assertIn("boolean false", root_account_locked_new)

        # Change IsRootAccountLocked property from the CLI
        # and make sure the change is reflected in the WebUI
        m.execute(set_root_account_locked_cmd("true"))
        # Go back to the root account subpage
        b.click('#root-account')
        b.wait_visible("#root-account-lock:checked")


if __name__ == '__main__':
    test_main()
